#include "stdafx.h"
#include "Animal.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CAnimal::CAnimal(void):m_Bitmap(NULL)
{
	m_Bitmap = new CCBitmap(_CHICKEN);
	m_Cx = ANIMAL_X;
	m_Cy = ANIMAL_Y;
	m_Egg_New = EGG_NEW;
	m_Egg_Next = EGG_NEW;
	m_iBlock = ANIMAL_OFFSET_MOVE;
	m_Direction = 1;
	m_TimeLife = ANIMAL_LIFETIME;
}

CAnimal::CAnimal(int x, int m_iAnimal_Type):m_Bitmap(NULL)
{
	m_Bitmap = new CCBitmap(m_iAnimal_Type);
	m_Cx = x;
	m_Cy = ANIMAL_Y;
	m_Egg_New = EGG_NEW;
	m_Egg_Next = EGG_NEW;
	m_iBlock = ANIMAL_OFFSET_MOVE;
	m_Direction = 1;
	m_TimeLife = ANIMAL_LIFETIME;
}
CAnimal::~CAnimal(void)
{
	
}

void CAnimal::Draw(CDC *pDC)
{	
	CDC dcMem;
	dcMem.CreateCompatibleDC (pDC);
	dcMem.SelectObject(&m_Bitmap->m_Bitmap);
	pDC->StretchBlt(m_Cx,m_Cy,ANIMAL_WIDTH,ANIMAL_HEIGHT,&dcMem,0,0,m_Bitmap->m_bmWidth,m_Bitmap->m_bmHeight,SRCCOPY);
	dcMem.DeleteDC();

	/*CDC memDC;
	memDC.CreateCompatibleDC(pDC);
	memDC.SelectObject(&m_Bitmap->m_Bitmap);
    CDC* dcMask = new CDC();
    CBitmap* bmMask = new CBitmap();
    dcMask->CreateCompatibleDC(pDC);
    bmMask->CreateBitmap(ANIMAL_WIDTH,ANIMAL_HEIGHT,1,1,0 );
    dcMask->SelectObject(&bmMask);
	dcMask->StretchBlt(m_Cx,m_Cy,ANIMAL_WIDTH,ANIMAL_HEIGHT,&memDC,0,0,m_Bitmap->m_bmWidth,m_Bitmap->m_bmHeight,SRCCOPY);
	pDC->StretchBlt(m_Cx,m_Cy,ANIMAL_WIDTH,ANIMAL_HEIGHT,dcMask,0,0,m_Bitmap->m_bmWidth,m_Bitmap->m_bmHeight,SRCPAINT);
	pDC->StretchBlt(m_Cx,m_Cy,ANIMAL_WIDTH,ANIMAL_HEIGHT,&memDC,0,0,m_Bitmap->m_bmWidth,m_Bitmap->m_bmHeight,SRCAND);

	dcMask->SelectObject(&m_Bitmap->m_Bitmap);
	bmMask->DeleteObject();
	dcMask->DeleteDC();
	memDC.DeleteDC();*/
}

int CAnimal::Animal_Proc()
{
	ANIMAL_MOVE();
	if(m_TimeLife == 0)//die
	{
		return 1; //  
	}
	m_TimeLife -= TIME_UPDATE;
	return 0;
}
void CAnimal::ANIMAL_MOVE()
{
	if(m_Direction == 1)
	{
		if(m_Cx + m_iBlock > ((GM_RIGHT-10)-ANIMAL_WIDTH))
		{
			m_Direction = -1;
		}
		else
		{
			m_Cx += m_iBlock;
		}
	}
	if(m_Direction == - 1)
	{
		if(m_Cx - m_iBlock < GM_LEFT)
		{
			m_Cx = 5;
			m_Direction = 1;
		}
		else
		{
			m_Cx -= m_iBlock;
		}
	}
}